﻿/*
 * Created by SharpDevelop.
 * User: tentus
 * Date: 2006-10-22
 * Time: 23:06
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;

namespace ActionManager
{
	/// <summary>
	/// Description of ActionDll.
	/// </summary>
	public class ActionDll
	{
		string actionDir = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath)+"\\Actions\\";
		
		[DllImport("kernel32",EntryPoint="LoadLibrary",SetLastError=true)]
	    static extern IntPtr LoadLibrary(string lpLibName); 
	
	    [DllImport("kernel32",EntryPoint="GetProcAddress",SetLastError=true)] 
	    static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); 
	
	    [DllImport("kernel32",EntryPoint="FreeLibrary",SetLastError=true)] 
	    static extern bool FreeLibrary(IntPtr hModule); 
	    
	    public struct SFilenameItem
	    {
	    	public string tekst;
	    	public string filename;
	    	public SFilenameItem(string _tekst, string _filename)
	    	{
	    		tekst    = _tekst;
	    		filename = _filename;
	    	}
	    };
				    
	    internal delegate string getText();
	    internal delegate string getSettings(string _settings, ref bool _zwroc);
	    internal delegate bool executeAction(string xml);				
						
		public ActionDll()
		{
		}
		
		IntPtr LoadLibraryDll(String _filename)
		{
			if (File.Exists(_filename)==false)return IntPtr.Zero;
			
			return LoadLibrary(_filename);
		}
		
		IntPtr GetProcAddressDll(IntPtr _hmodule, string _name)
		{
			IntPtr zwroc = IntPtr.Zero;
			
			zwroc = GetProcAddress(_hmodule, "_"+_name);
	        if (zwroc ==IntPtr.Zero)
	        	zwroc = GetProcAddress(_hmodule, _name);
	        
	        return zwroc;
		}
		
		private string getActionTexttDll(string _file_name)
		{
			string text = "";
			
			IntPtr hModule = LoadLibraryDll(actionDir+_file_name);
			if (hModule==IntPtr.Zero) return "";
			
			IntPtr pfn     = GetProcAddressDll(hModule, "getText");
	        
	        //string sciezka = actionDir+_file_name;
	        //if (File.Exists(sciezka)==false)return "";
	
	        //hModule = LoadLibrary(sciezka);  
	        //pfn = GetProcAddress(hModule, "_getText");
	        //if (pfn ==IntPtr.Zero)
	        //	pfn = GetProcAddress(hModule, "getText");
	        if (pfn!=IntPtr.Zero)
	        {		        
	        	getText getTextDll = (getText)Marshal.GetDelegateForFunctionPointer(pfn, typeof(getText));		        
		        text = getTextDll();
	        }	        	        	        
	        
	        FreeLibrary(hModule);
	        
	        return text;
		}
		
		public void GetActionList(List<SFilenameItem> _list)
		{
			string []fileList = Directory.GetFiles(actionDir);						
			
			for (int i=0; i<fileList.Length; ++i)
			{
				string name = getActionTexttDll(Path.GetFileName(fileList[i]));
				if (name=="")continue;
				
				_list.Add(new SFilenameItem(name, Path.GetFileName(fileList[i])));
			}
		}
		
		public bool ExecuteAction(string _file_name, string _settings)
		{
			if (_file_name=="")return false;
			
			IntPtr hModule = IntPtr.Zero;
	        IntPtr pfn = IntPtr.Zero;
	        bool zwroc = false;
	
	        string sciezka = actionDir+_file_name;
	        if (File.Exists(sciezka)==false)return false;
	        
	        hModule = LoadLibrary(sciezka);
	        pfn = GetProcAddress(hModule, "_executeAction");  
	        if (pfn==IntPtr.Zero)
	        	pfn = GetProcAddress(hModule, "executeAction");  
	        
	        if (pfn!=IntPtr.Zero)
	        {		        	        	
	        	executeAction exe = (executeAction)Marshal.GetDelegateForFunctionPointer(pfn, typeof(executeAction));		        
		        zwroc = exe(_settings);
	        }	        	        	        
	        
	        FreeLibrary(hModule);
	        
	        return zwroc;
		}
		
		public bool GetActionSettings(string _file_name, ref string _settings)
		{			
			string settings = _settings;			
			IntPtr hModule  = IntPtr.Zero;
	        IntPtr pfn      = IntPtr.Zero;
	        bool zwroc      = false;
	
	        string sciezka = actionDir+_file_name;
	        if (File.Exists(sciezka)==false)return false;
	        
	        hModule = LoadLibrary(sciezka);  
	        pfn = GetProcAddress(hModule, "_getSettings");  
	        if (pfn==IntPtr.Zero)
	        	pfn = GetProcAddress(hModule, "getSettings");  
	        if (pfn!=IntPtr.Zero)
	        {		        
	        	getSettings getSettingsDll = (getSettings)Marshal.GetDelegateForFunctionPointer(pfn, typeof(getSettings));		        
		        settings = getSettingsDll(settings, ref zwroc);
	        }	        	        	        
	        
	        FreeLibrary(hModule);
	        
	        if (zwroc)
	        	_settings = settings;
	        
	        return zwroc;
		}
		
		
	}
}


